package com.tedu._04mybatis.mapper;

import com.tedu._04mybatis.pojo.User;
import com.tedu._04mybatis.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

//@Component
public class UserMapperImpl implements UserMapper{
    //    @Autowired
    private IDBUtil dbUtil;
    @Override
    public int insertUser(User user) {
        try (Connection connection = dbUtil.getConnection();){
            String sql = "INSERT INTO user(username,password,age) " +
                    "VALUES(?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setInt(3, user.getAge());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
